package com.best.guanfengji.serial_port

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.best.guanfengji.R
import com.best.guanfengji.base.BaseDialogFragment
import com.blankj.utilcode.util.ConvertUtils

class MyAlertDialog(val content: String,val onAlertClickListener:OnAlertClickListener): BaseDialogFragment() {

    override fun onStart() {
        super.onStart()
        //设置dialog的大小
        val lp: WindowManager.LayoutParams? = dialog?.window?.attributes
        lp?.width = ConvertUtils.dp2px(360f)
        lp?.height = ConvertUtils.dp2px(300f)
        dialog?.window?.attributes = lp
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.setCancelable(false)
    }

    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_alert, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val ib_close = view.findViewById<ImageView>(R.id.ib_close)
        val tv_content = view.findViewById<TextView>(R.id.tv_content)
        val tv_left = view.findViewById<TextView>(R.id.tv_left)
        val tv_right = view.findViewById<TextView>(R.id.tv_right)

        ib_close.setOnClickListener{
            dismiss()
        }
        tv_left.setOnClickListener {
            dismiss()
        }
        tv_content.text = content
        tv_right.setOnClickListener {
            onAlertClickListener.onRightClick()
            dismiss()
        }

    }

    interface OnAlertClickListener{
        fun onRightClick()
    }

}